<?php
// Use this file to define customized helper functions, filters or 'hacks' defined
// specifically for use in your Omeka theme. Note that helper functions that are
// designed for portability across themes should be grouped into a plugin whenever
// possible. Ideally, you should namespace these with your theme name.

function custom_show_item_metadata_poincare(array $options = array(), $item = null)
{
    if (!$item) {
        $item = get_current_item();
    }
    if ($dcFieldsList = get_theme_option('display_dublin_core_fields')) {

        $otherElementSets = array();

        $elementSets = get_db()->getTable('ElementSet')->findForItems();
        foreach ($elementSets as $set) {
            if ($set->name == 'Dublin Core') continue;
            $otherElementSets[] = $set->name;
        }

        $html = '<h5>Notice détaillée</h2>';
        $html .= '<table>';
        $dcFields = explode(',', $dcFieldsList);
        foreach ($dcFields as $field) {
            $field = trim($field);
            if (element_exists('Dublin Core', $field)) {
                if ($fieldValues = item('Dublin Core', $field, 'all')) {
                    $html .= '<tr><td>'.__($field).'</td><td>';
                    foreach ($fieldValues as $key => $fieldValue) {
                        if (!item_field_uses_html('Dublin Core', $field, $key)) {
                            $fieldValue = $fieldValue.'; ';
                        }
                        $html .= $fieldValue;
                    }
                    $html .= '</td></tr>';
                }
            }
        }
        $html .= '</table>';
        // non DC metadata with item-metadata.php
        $html .= show_item_metadata(array('show_element_sets' => $otherElementSets));
        return $html;
    } else {
        return show_item_metadata($options, $item);
    }
}
function display_files_for_item_poincare($options = array(), $wrapperAttributes = array('class'=>'item-file'), $item = null)
{
    if(!$item) {
        $item = get_current_item();
    }
    return display_files_poincare($item->Files, $options, $wrapperAttributes);
}
function display_files_poincare($files, $props = array(), $wrapperAttributes = array('class'=>'item-file'))
{
    require_once 'Media_Poincare.php';
    $helper = new Omeka_View_Helper_Media;
    $output = '';
    foreach ($files as $file) {
    		$props = array_merge($props, array('title'=>$file->original_filename, 'alt'=>$file->original_filename));
        $output .= $helper->media($file, $props, $wrapperAttributes);
    }
    return $output;
}

/**
// Modifie de /application/helpers/TagFunctions.php
 */
function item_tags_as_string_poincare($delimiter = null, $order = 'alpha',  $tagsAreLinked = true, $item=null, $limit=null)
{
    // Set the tag_delimiter option if no delimiter was passed.
    if (is_null($delimiter)) {
        $delimiter = get_option('tag_delimiter') . ' ';
    }

    if (!$item) {
        $item = get_current_item();
    }
    $tags = get_tags(array('sort'=>$order, 'record'=>$item), $limit);
    $urlToLinkTo = ($tagsAreLinked) ? uri('items/browse?tags=') : null;
    return tag_string($tags, $urlToLinkTo, $delimiter);
}
function item_citation_poincare($item = null)
{
    if(!$item) {
        $item = get_current_item();
    }

    $creator    = strip_formatting(item('Dublin Core', 'Creator', array(), $item));
    $title      = strip_formatting(item('Dublin Core', 'Title', array(), $item));
    $siteTitle  = strip_formatting(settings('site_title'));
    $itemId     = item('id', null, array(), $item);
    $accessDate = date('F j, Y');
    $uri        = html_escape('http://purl.oclc.org/net/henri-poincare/'.$item->id);

    $cite = '';
    if ($creator) {
        $cite .= "$creator, ";
    }
    if ($title) {
        $cite .= "&#8220;$title,&#8221; ";
    }
    if ($siteTitle) {
        $cite .= "<em>$siteTitle</em>, ";
    }
    $cite .= "accessed $accessDate, ";
    $cite .= "<a href=\"$uri\">$uri</a>";

    return apply_filters('item_citation', $cite, $item);
}
function display_random_featured_item_poincare($withImage = null)
{
    $html = '<h5>Actualités</h5>';
    $html .= display_random_featured_items_poincare('5', $withImage);
    return $html;
}
function exhibit_builder_display_random_featured_exhibit_poincare()
{
    $html = '<div id="featured-exhibit">';
    $featuredExhibit = exhibit_builder_random_featured_exhibit();
    $html .= '<h5>L\'exposition</h5>';
    if ($featuredExhibit) {
       $html .= '<h3>' . exhibit_builder_link_to_exhibit($featuredExhibit) . '</h3>'."\n";
       $html .= '<p>'.snippet_by_word_count(exhibit('description', array(), $featuredExhibit),'80').'</p>';
    } else {
       $html .= '<p>' . __('You have no featured exhibits.') . '</p>';
    }
    $html .= '</div>';
    $html = apply_filters('exhibit_builder_display_random_featured_exhibit', $html);
    return $html;
}
function display_partners_poincare()
{
    $html = '<div id="featured-exhibit">'."\n";
    $html .= '<h5>Tutelles et partenaires</h5>'."\n";
    $html .= '<span class="display-partners-logo">
    					<a target="_blank" title="Centre National de la recherche Scientifique" href="http://www.cnrs.fr">
							<img alt="CNRS" src="themes/poincare/images/logo-CNRS-32.png"></a>
							</span>'."\n";
    $html .= '<span class="display-partners-logo">
    					<a target="_blank" title="Université de Lorraine" href="http://www.univ-lorraine.fr/">
    					<img alt="Université de Lorraine" src="themes/poincare/images/logo-udl-small.jpg"></a>
							</span>'."\n";
    $html .= '<span class="display-partners-logo">
    					<a target="_blank" title="MSH Lorraine" href="http://www.msh-lorraine.fr/">
    					<img alt="MSH Lorraine" src="themes/poincare/images/logo-msh-lorraine_110.jpg"></a>
    					</span>'."\n";
    $html .= '<span class="display-partners-logo">
							<a target="_blank" title="tge ADONIS" href="http://www.tge-adonis.fr/">
							<img alt="tge ADONIS" src="themes/poincare/images/logo-adonis_140.png"></a>
							</span>'."\n";
    $html .= '<span class="display-partners-logo">
							<a target="_blank" title="Institut Henri Poincaré" href="http://www.ihp.fr/">
							<img alt="Institut Henri Poincaré" src="themes/poincare/images/logo-ihp_50.jpg"></a>
							</span>'."\n";
    $html .= '</div>';
    return $html;
}
function display_random_featured_items_poincare($num = 5, $hasImage = null)
{
    $html = '';
    if ($randomFeaturedItems = random_featured_items($num, $hasImage)) {
        foreach ($randomFeaturedItems as $randomItem) {
            $itemTitle = item('Dublin Core', 'Title', array(), $randomItem);
						$html .= '<div class="random_featured_item">';
            $html .= '<h3>' . link_to_item($itemTitle, array(), 'show', $randomItem) . '</h3>';
            if (item_has_thumbnail($randomItem)) {
                $html .= link_to_item(item_square_thumbnail(array(), 0, $randomItem), array('class'=>'image'), 'show', $randomItem);
            }
            if ($itemDescription = item('Dublin Core', 'Description', array('snippet'=>300), $randomItem)) {
                $html .= '<p>' . $itemDescription . '</p>';
            }
						$html .= '</div>';
        }
    } else {
        $html .= '<p>'.__('No featured items are available.').'</p>';
    }
    return $html;
}
function exhibit_builder_section_nav_poincare($exhibit = null, $homeMsg = 'Home')
{
    if (!$exhibit) {
        if (!($exhibit = exhibit_builder_get_current_exhibit())) {
            return;
        }
    }
    $html = '<ul class="exhibit-section-nav">';
    $html .= '<li>'.link_to_exhibit($homeMsg, array('class'=>'exhibit-section-title')).'</li>';
    foreach ($exhibit->Sections as $key => $exhibitSection) {
        if ($exhibitSection->hasPages()) {
            $html .= '<li' . (exhibit_builder_is_current_section($exhibitSection) ? ' class="current"' : ''). '><a class="exhibit-section-title" href="' . html_escape(exhibit_builder_exhibit_uri($exhibit, $exhibitSection)) . '">' . html_escape($exhibitSection->title) . '</a></li>';
        }
    }
    $html .= '</ul>';
    $html = apply_filters('exhibit_builder_section_nav', $html, $exhibit);
    return $html;
}
function exhibit_builder_link_to_next_exhibit_page_poincare($text = null, $props = array(), $exhibitPage = null)
{
    if ($text === null) {$text = __('Next Page &rarr;');}
    if (!$exhibitPage) {$exhibitPage = exhibit_builder_get_current_page();}

    $exhibitSection = exhibit_builder_get_exhibit_section_by_id($exhibitPage->section_id);
    $exhibit = exhibit_builder_get_exhibit_by_id($exhibitSection->exhibit_id);

    if(!isset($props['class'])) {$props['class'] = 'next-page';}

    // if page object exists, grab link to next exhibit page if exists. If it doesn't, grab
    // a link to the first page on the next exhibit section, if it exists.
    if ($nextPage = $exhibitPage->next()) {
        return exhibit_builder_link_to_exhibit_poincare($exhibit, $text, $props, $exhibitSection, $nextPage);
    } elseif ($nextSection = $exhibitSection->next()) {
        return exhibit_builder_link_to_exhibit_poincare($exhibit, $text, $props, $nextSection);
    }
}
function exhibit_builder_link_to_exhibit_poincare($exhibit = null, $text = null, $props = array(), $exhibitSection = null, $exhibitPage = null)
{
    if (!$exhibit) {
        $exhibit = exhibit_builder_get_current_exhibit();
    }
    $uri = exhibit_builder_exhibit_uri($exhibit, $exhibitSection, $exhibitPage);
    $text = !empty($text) ? $text : html_escape($exhibit->title);
    return '<a href="' . html_escape($uri) .'" '. _tag_attributes($props) . '><span>' . $text . '</span></a>';
}
function exhibit_builder_link_to_previous_exhibit_page_poincare($text = null, $props = array(), $exhibitPage = null)
{
    if ($text === null) {$text = __('&larr; Previous Page');}
    if (!$exhibitPage) {$exhibitPage = exhibit_builder_get_current_page();}

    $exhibitSection = exhibit_builder_get_exhibit_section_by_id($exhibitPage->section_id);
    $exhibit = exhibit_builder_get_exhibit_by_id($exhibitSection->exhibit_id);

    if(!isset($props['class'])) {$props['class'] = 'previous-page';}

    // If page object exists, grab link to previous exhibit page if exists. If it doesn't, grab
    // a link to the last page on the previous exhibit section, if it exists.
    if ($previousPage = $exhibitPage->previous()) {
        return exhibit_builder_link_to_exhibit_poincare($exhibit, $text, $props, $exhibitSection, $previousPage);
    } elseif ($previousSection = $exhibitSection->previous()) {
        if ($pages = $previousSection->Pages) {
            $page = end($pages);
            return exhibit_builder_link_to_exhibit_poincare($exhibit, $text, $props, $previousSection, $page);
        }
    }
}
/* Displays logo of the theme in header.php */
function custom_display_logo_poincare()
{
    if(function_exists('get_theme_option')) {
        $logo = get_theme_option('Logo');
        if ($logo) {
            $storage = Zend_Registry::get('storage');
            $uri = $storage->getUri($storage->getPathByType($logo, 'theme_uploads'));
            return '<img src="'.$uri.'" title="'.settings('site_title').'" alt="'.settings('site_title').'"/>';
        }
    }
    return null;
}
/* Displays secondary navigation */
function secondary_poincare()
{
	$html = "
  <div class=\"items-nav\">
		<form id=\"simple-search\" method=\"get\" action=\"".WEB_ROOT."/items/browse\">
		<ul class=\"secondary-nav\">
			<li><strong>Rechercher :</strong></li>
			<li>
				<input id=\"search\" class=\"textinput\" type=\"text\" value=\"";
				if(isset($_GET['search'])) $html .= $_GET['search'];
				$html .= "\" name=\"search\">
				<input id=\"submit_search\" type=\"submit\" value=\"ok\" name=\"submit_search\">
			</li>
			<li>";
			$html .= link_to_advanced_search();
			$html .= "</li>";
	    $html .= custom_nav_items(array(__('Tous les contenus') => uri('items')));
	    $html .= "
    </ul>
		</form>
	</div>
	<div class=\"items-nav\" id=\"secondary-nav\">
	<ul class=\"secondary-nav\">
		<li><strong>Parcourir :</strong></li>";
	    $html .= custom_nav_items(array(
	    __('Par types de contenus') => uri('item-types'),
	    __('Par collections') => uri('collections'),
	    __('Par auteurs') => uri('category/creator'),
	    __('Par thèmes') => uri('category/subject'),
	    __('Par mots-clés') => uri('items/tags?sort=alpha'),
	    __('Par dates') => uri('category/date')
	    ));
	    $html .= "
	</ul>
	</div>
	";
	return $html;
}
function display_feed($feedUrl, $num = 3) {
    try {
        $feed = Zend_Feed_Reader::import($feedUrl);
    } catch (Zend_Feed_Exception $e) {
        echo '<p>Feed not available.</p>';
        return;
    }

    $posts = 0;
    foreach ($feed as $entry) {
        if (++$posts > $num) break;
        $title = $entry->getTitle();
        $link = $entry->getLink();
        $description = $entry->getDescription();
        echo "<p><a href=\"$link\">$title</a></p>"
           . "<p>$description <a href=\"$link\">...more</a></p>";
    }
}